package com.gosophia.accountManagement.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.web.action.BaseAjaxAction;
import com.gosophia.commons.web.logAnnotation.LogRecord;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.userManagement.dto.UserAccountDTO;
import com.gosophia.userManagement.ws.UserManagementWebService;

/**
 * 删除账号
 * @author mayue
 *
 */
@Controller("DeleteUserAccountAction")
@Scope("prototype")
public class DeleteUserAccountAction extends BaseAjaxAction<UserAccountDTO> {
    
    private static final long serialVersionUID = 1L;

    @Autowired
    private UserManagementWebService userManagementWebService;
    
    private UserAccountDTO userAccountDTO;

    @Override
    @LogRecord(eventAction="账号管理--删除账号",eventTarget="userAccountDTO")
    public String _execute() throws Exception {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userAccountName = userDetails.getUserAccountDTO().getUserAccountName();
        if(userAccountName!=null && userAccountDTO!=null && userAccountName.equals(userAccountDTO.getUserAccountName())){
            throw new BusinessException("当前登录账号不能删除", "JC2_OA_UA_04");
//            super.getRequestResult().setMessage("当前登录账号不能删除");
//            super.getRequestResult().setRsltCode("1");
//            return ERROR;
        }
        userManagementWebService.removeUserAccount(userAccountDTO.getUserAccountId(), userAccountDTO.getVersion());
        super.getRequestResult().setMessage("账号"+userAccountDTO.getUserAccountName()+"删除成功");
        super.getRequestResult().setRsltCode("0");
        
        return SUCCESS;
    }


    public UserAccountDTO getUserAccountDTO() {
        return userAccountDTO;
    }


    public void setUserAccountDTO(UserAccountDTO userAccountDTO) {
        this.userAccountDTO = userAccountDTO;
    }


    @Override
    public UserAccountDTO getDetailData() {
        // TODO Auto-generated method stub
        return null;
    }


}
